package com.ruoyi.project.management.approve.controller;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.management.delay.domain.TaskDelay;
import com.ruoyi.project.management.delay.mapper.ManagementTaskMapper;
import com.ruoyi.project.management.delay.service.ManagementDelayService;
import com.ruoyi.project.management.delay.service.ManagementTaskService;
import com.ruoyi.project.management.tranfer.domain.TaskTransfer;
import com.ruoyi.project.system.post.domain.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/management/approve")
public class ManagementApproveController extends BaseController {


    @Autowired
    private ManagementDelayService managementDelayService;

    @Autowired
    private ManagementTaskMapper managementTaskMapper;


    private String prefix = "management/approve";


    /**
     * 跳转到延期审批
     * @return
     */
    @GetMapping()
    public String task()
    {
        return prefix + "/approve";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TaskDelay taskDelay)
    {
        startPage();
        List<TaskDelay> list = managementDelayService.selectMore(taskDelay);
        return getDataTable(list);
    }


    /**
     * 修改任务
     */
    @GetMapping("/edit/{delay_Id}")
    public String edit(@PathVariable("delay_Id") Integer delay_Id, ModelMap mmap)
    {
        mmap.put("delay", managementDelayService.selectDelayById(delay_Id));
        return prefix + "/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TaskDelay taskDelay)
    {

        taskDelay.setApproved_time(new Date());
        System.out.println(taskDelay);

        if(taskDelay.getPass() == 1 && taskDelay.getAppointed_time() != null) {
            System.out.println("+++++++++++++++++++Yes++++++++++++++++++");
            managementTaskMapper.updateByEndTime(taskDelay.getAppointed_time(), taskDelay.getTask_id());
        }
        ShiroUtils.clearCachedAuthorizationInfo();



        return toAjax(managementDelayService.update(taskDelay));
    }





    /**
     * 根据延期id查看任务信息
     * @param delayId 延期id
     * @param mmap
     * @return
     */
    @GetMapping("/show/{delay_Id}")
    public String show(@PathVariable("delay_Id") Integer delayId, ModelMap mmap)
    {
        mmap.put("delay", managementDelayService.selectDelayById(delayId));
        return prefix + "/show";
    }



}
